/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formLapHoaDonBanSach.java
 *
 * Created on Jun 22, 2013, 3:38:47 PM
 */

package GUI;

import BUS.DonHangBUS;
import BUS.KhachHangBUS;
import BUS.KhoanNoBUS;
import BUS.SachBUS;
import Entity.CTDonHang;
import Entity.DonHang;
import Entity.KhachHang;
import Entity.KhoanNo;
import Util.ComboBoxTagModel;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author copang
 */
public class formLapHoaDonBanSach extends javax.swing.JPanel {

    private ComboBoxTagModel cmbTenSachModels;  
    private ComboBoxTagModel cmbKhachHangModels;
    
    private SachBUS sachBUS;
    private KhachHangBUS khachHangBUS;
    private DonHangBUS donHangBUS;
    private KhoanNoBUS khoanNoBUS;
    
    private ArrayList listSach;
        
    /** Creates new form formLapHoaDonBanSach */
    public formLapHoaDonBanSach() {
        initComponents();
        sachBUS = new BUS.SachImpl();
        khachHangBUS = new BUS.KhachHangImpl();
        donHangBUS = new BUS.DonHangImpl();
        khoanNoBUS = new BUS.KhoanNoImpl();
        
        txtSoLuong.setTransferHandler(null);
        txtTienTra.setTransferHandler(null);

        initDate();
        initTenNV();
        loadKhachHang();
        loadTenSach();
        
        setEnableControl(false);
    }
    
    protected void initDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        txtNgayLap.setText(dateFormat.format(date));
    }
    
    protected void initTenNV() {
        txtNhanVien.setText(Util.InstanceData.loginNhanVien.getHoTenNV());
    }

    protected void loadTenSach() {
        listSach = sachBUS.getAllWithTenSach();        
        
        cmbTenSachModels = new ComboBoxTagModel();
        for (int i=0; i<listSach.size(); i++) {
            Object[] tg = (Object[]) listSach.get(i);
            cmbTenSachModels.addElement(tg[5].toString(), 0);
        }
        
        cmbTenSach.setModel(cmbTenSachModels);
        loadThongTinSach(0);
    }
    
    protected void loadKhachHang() {
        cmbKhachHangModels = new ComboBoxTagModel();

        // get list KhachHang
        ArrayList list = khachHangBUS.getKhachHang();
        for(int i = 0; i < list.size(); i++) {
            KhachHang kh = (KhachHang)list.get(i);
            cmbKhachHangModels.addElement(kh.getHoTenKH(), kh.getMaKhachHang());
        }
        cmbKhachHang.setModel(cmbKhachHangModels);
    }
    
    protected void loadThongTinSach(int index) {
        if(listSach.size() == 0)
            return;
        if (index < 0) return;
        
        Object[] data = (Object[]) listSach.get(index);
        txtNXB.setText(data[6].toString());
        txtNamXB.setText(data[3].toString());
        txtDonGia.setText(data[2].toString());
        txtToKho.setText(data[4].toString());
        
    }
    
    protected void updateTonKho(int index, int soLuong) {
        Object[] obj = (Object[]) listSach.get(index);
        int tonKho = (Integer) obj[4];
        tonKho -= soLuong;
        obj[4] = tonKho;
        
    //    txtToKho.setText(obj[4].toString());
        loadThongTinSach(cmbTenSach.getSelectedIndex());
    }
    
    protected void updateTonKhoWithMaSach(int maSach, int soLong) {
        int index = 0;
        for (int i=0; i<listSach.size(); i++) {
            Object[] obj = (Object[]) listSach.get(i);
            
            if ( (Integer) obj[0] == maSach) {
                index = i;
                updateTonKho(index, -soLong);
                break;
            }
        }        
    }
    
    protected void updateThanhTien(int thanhTien) {
        int tien = Integer.parseInt(txtThanhTien.getText());
        tien += thanhTien;
        txtThanhTien.setText(String.valueOf(tien));
        
        updateNo();
    }
    
    protected void updateNo() {
        int no = Integer.parseInt(txtTienNo.getText());
        int tra = Integer.parseInt(txtTienTra.getText());
        int tong = Integer.parseInt(txtThanhTien.getText());
        
        no = tong - tra;
        txtTienNo.setText(String.valueOf(no));
    }
    
    protected void addDataTable(int index) {
        DefaultTableModel model = (DefaultTableModel) tbChiTiet.getModel();
        
        Object[] obj = (Object[]) listSach.get(index);
        int maSach = (Integer) obj[0];
        int maDauSach = (Integer) obj[1];
        String tenSach = obj[5].toString();
        int soLuong = Integer.parseInt(txtSoLuong.getText());
        int donGia = Integer.parseInt(txtDonGia.getText());
        int thanhTien = soLuong * donGia;
        
        Object[] row = {
            maSach,
            maDauSach,
            tenSach,
            soLuong,
            donGia,
            thanhTien
        };
        
        model.addRow(row);
        
        updateTonKho(index, soLuong);
        updateThanhTien(thanhTien);
    }
    
    protected void themChiTiet() {
        int index = cmbTenSach.getSelectedIndex();
        
        if (index == -1)
            return;
        
        int mua = Integer.parseInt(txtSoLuong.getText());
        int ton = Integer.parseInt(txtToKho.getText());
        
        if ( mua > ton ) {
            JOptionPane.showMessageDialog(this, "Số lượng mua quá lớn");
            return;
        }           
        
        addDataTable(index);
    }
    
    protected void deleteTableRow(int index) {
        DefaultTableModel model = (DefaultTableModel) tbChiTiet.getModel();
        
        int soLuong = (Integer) model.getValueAt(index, 3);
        int maSach = (Integer) model.getValueAt(index, 0);
        
        model.removeRow(index);
        updateTonKhoWithMaSach(maSach, soLuong);
    }
    
    protected boolean createDonHang( DonHang data ) {
        int maKhach = (Integer) cmbKhachHangModels.getTagItem(cmbKhachHang.getSelectedIndex());
        data.setMaKhachHang(maKhach);
        data.setNgayHoaDon(txtNgayLap.getText());
        data.setTongTien( Integer.parseInt(txtThanhTien.getText()) );
        
        return donHangBUS.add(data);
    }
    
    protected boolean createCTDonHang( int index, int maDon ) {
        DefaultTableModel model = (DefaultTableModel) tbChiTiet.getModel();
        CTDonHang chiTiet = new CTDonHang(
                maDon, 
                (Integer) model.getValueAt(index, 0), 
                (Integer) model.getValueAt(index, 4), 
                (Integer) model.getValueAt(index, 3));
                
        if (!donHangBUS.addDetail(chiTiet))
            return false;
        
        
        // update soLuongTon
        return sachBUS.subSoLuongTon(chiTiet.getMaSach(), chiTiet.getSoLuongBan());
    }
    
    protected boolean save() {
        // check conditions
        
        // create DonHang
        DonHang donHang = new DonHang();
        if (!createDonHang(donHang))
            return false;
        
        // select each table row
        for (int i=0; i < tbChiTiet.getRowCount(); i++) {
            
            // create ChiTiet_DonHang
            if (!createCTDonHang(i, donHang.getMaDonHang()))
                return false;
            
        }        
        
        // check if tienNo > 0
        int tienNo = Integer.parseInt(txtTienNo.getText());
        if (tienNo > 0) {
            
            // create KhoanNo
            KhoanNo khoanNo = new KhoanNo(0, donHang.getMaDonHang(), 
                    donHang.getMaDonHang(), 
                    Integer.parseInt(txtTienNo.getText()), 
                    txtNgayLap.getText(), "");
            if (!khoanNoBUS.add(khoanNo))
                return false;
        
            // update TienNo of KhachHang
            if (!khachHangBUS.addTienNo(donHang.getMaKhachHang(), tienNo))
                return false;
        }
        return true;
    }
    
    protected void setEnableControl(boolean isEnable) {
        txtSoLuong.setEnabled(isEnable);
        btnThemChiTiet.setEnabled(isEnable);
        txtTienTra.setEnabled(isEnable);
        btnLuuPhieu.setEnabled(isEnable);
        btnXoaPhieu.setEnabled(isEnable);
    }
    
    protected void clearAll() {
        txtSoLuong.setText("0");
        txtTienTra.setText("0");
        txtThanhTien.setText("0");
        txtTienNo.setText("0");
        
        DefaultTableModel model = (DefaultTableModel) tbChiTiet.getModel();
        for (int i=0; i<tbChiTiet.getRowCount(); i++) {
            model.removeRow(i);
        }
    }
    
    protected void reloadData() {
        loadKhachHang();
        loadTenSach();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel14 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtToKho = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbChiTiet = new javax.swing.JTable();
        txtThanhTien = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtNhanVien = new javax.swing.JTextField();
        txtNgayLap = new javax.swing.JTextField();
        txtDonGia = new javax.swing.JTextField();
        txtTienTra = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtTienNo = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        cmbTenSach = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cmbKhachHang = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        btnThemChiTiet = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txtNXB = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        txtNamXB = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        btnThemPhieu = new javax.swing.JButton();
        btnLuuPhieu = new javax.swing.JButton();
        btnXoaPhieu = new javax.swing.JButton();
        btnCapNhat = new javax.swing.JButton();
        jLabel21 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();

        jLabel14.setText("VND");

        jLabel16.setText("Thành tiền");

        txtToKho.setEditable(false);
        txtToKho.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel17.setText("Nhân viên lập hóa đơn :");

        tbChiTiet.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã sách", "Mã đầu sách", "Tên sách", "Số lượng (Quyển)", "Đơn giá (VND)", "Thành Tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbChiTiet.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbChiTietKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbChiTiet);

        txtThanhTien.setEditable(false);
        txtThanhTien.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtThanhTien.setText("0");

        jLabel12.setText("Tên khách hàng :");

        txtNhanVien.setEditable(false);
        txtNhanVien.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtNgayLap.setEditable(false);
        txtNgayLap.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtDonGia.setEditable(false);
        txtDonGia.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtTienTra.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtTienTra.setText("0");
        txtTienTra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtTienTraKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTienTraKeyTyped(evt);
            }
        });

        jLabel11.setText("VND");

        jLabel8.setText("Tiền khách trả :");

        jLabel7.setText("Tiền nợ :");

        txtTienNo.setEditable(false);
        txtTienNo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtTienNo.setText("0");

        jLabel13.setText("Tên sách :");

        jLabel10.setText("VND");

        cmbTenSach.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cmbTenSach.setOpaque(false);
        cmbTenSach.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbTenSachItemStateChanged(evt);
            }
        });

        jLabel9.setText("Quyển");

        cmbKhachHang.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cmbKhachHang.setOpaque(false);

        jLabel5.setText("Đơn giá :");

        btnThemChiTiet.setText("Thêm chi tiết");
        btnThemChiTiet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemChiTietActionPerformed(evt);
            }
        });

        jLabel18.setText("Ngày lập hóa đơn :");

        jLabel6.setText("Tồn kho :");

        jLabel15.setText("NXB :");

        txtNXB.setEditable(false);

        jLabel19.setText("Năm XB :");

        txtNamXB.setEditable(false);
        txtNamXB.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel20.setText("VND");

        jToolBar1.setRollover(true);

        btnThemPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Add.png"))); // NOI18N
        btnThemPhieu.setText("Thêm Phiếu");
        btnThemPhieu.setFocusable(false);
        btnThemPhieu.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnThemPhieu.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnThemPhieu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnThemPhieuMouseClicked(evt);
            }
        });
        jToolBar1.add(btnThemPhieu);

        btnLuuPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/save.png"))); // NOI18N
        btnLuuPhieu.setText("Lưu Phiếu");
        btnLuuPhieu.setFocusable(false);
        btnLuuPhieu.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnLuuPhieu.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnLuuPhieu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLuuPhieuMouseClicked(evt);
            }
        });
        jToolBar1.add(btnLuuPhieu);

        btnXoaPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/delete.png"))); // NOI18N
        btnXoaPhieu.setText("Xóa Phiếu");
        btnXoaPhieu.setFocusable(false);
        btnXoaPhieu.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnXoaPhieu.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnXoaPhieu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnXoaPhieuMouseClicked(evt);
            }
        });
        jToolBar1.add(btnXoaPhieu);

        btnCapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/update.png"))); // NOI18N
        btnCapNhat.setText("Cập nhật");
        btnCapNhat.setFocusable(false);
        btnCapNhat.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnCapNhat.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCapNhat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCapNhatMouseClicked(evt);
            }
        });
        jToolBar1.add(btnCapNhat);

        jLabel21.setText("Mua :");

        txtSoLuong.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtSoLuong.setText("0");
        txtSoLuong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSoLuongKeyTyped(evt);
            }
        });

        jLabel22.setText("Quyển");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 868, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel12)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbKhachHang, 0, 140, Short.MAX_VALUE)
                            .addComponent(txtNhanVien, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                            .addComponent(txtNgayLap, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel19)
                            .addComponent(jLabel15)
                            .addComponent(jLabel13)
                            .addComponent(jLabel21))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtNamXB, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtToKho, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel9))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel10))
                                    .addComponent(txtNXB, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                                    .addComponent(cmbTenSach, 0, 195, Short.MAX_VALUE))
                                .addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel8))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTienNo, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                    .addComponent(txtTienTra, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                    .addComponent(txtThanhTien, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel20)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel22)
                                .addGap(18, 18, 18)
                                .addComponent(btnThemChiTiet)
                                .addGap(0, 222, Short.MAX_VALUE)))))
                .addGap(28, 28, 28))
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 924, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16)
                            .addComponent(jLabel20))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNgayLap)
                            .addComponent(jLabel18)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel13)
                        .addComponent(cmbTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNXB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNamXB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel19)
                            .addComponent(cmbKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTienNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel11))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTienTra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addComponent(jLabel14)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel17)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtToKho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22)
                    .addComponent(btnThemChiTiet))
                .addGap(19, 19, 19)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addGap(26, 26, 26))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmbTenSachItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbTenSachItemStateChanged
        int index = cmbTenSach.getSelectedIndex();
        if (index >= 0)
            loadThongTinSach(index);
    }//GEN-LAST:event_cmbTenSachItemStateChanged

    private void btnThemChiTietActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemChiTietActionPerformed
        themChiTiet();
    }//GEN-LAST:event_btnThemChiTietActionPerformed

    private void txtTienTraKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTienTraKeyReleased
        updateNo();
    }//GEN-LAST:event_txtTienTraKeyReleased

    private void tbChiTietKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbChiTietKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            int index = tbChiTiet.getSelectedRow();
            if (index != -1) {
                deleteTableRow(index);
            }
        }
    }//GEN-LAST:event_tbChiTietKeyPressed

    private void btnCapNhatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCapNhatMouseClicked
        reloadData();
    }//GEN-LAST:event_btnCapNhatMouseClicked

    private void btnLuuPhieuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLuuPhieuMouseClicked
        if (save()) {
            JOptionPane.showMessageDialog(this, "Đã lưu phiếu");
            clearAll();
            setEnableControl(false);
            reloadData();
        } else {
            JOptionPane.showMessageDialog(this, "Không thể lưu phiếu");
        }
    }//GEN-LAST:event_btnLuuPhieuMouseClicked

    private void btnThemPhieuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnThemPhieuMouseClicked
        setEnableControl(true);
    }//GEN-LAST:event_btnThemPhieuMouseClicked

    private void btnXoaPhieuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnXoaPhieuMouseClicked
        clearAll();
        setEnableControl(false);
        reloadData();
    }//GEN-LAST:event_btnXoaPhieuMouseClicked

    private void txtTienTraKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTienTraKeyTyped
        Util.InstanceData.check(evt);
    }//GEN-LAST:event_txtTienTraKeyTyped

    private void txtSoLuongKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoLuongKeyTyped
        Util.InstanceData.check(evt);
    }//GEN-LAST:event_txtSoLuongKeyTyped


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnLuuPhieu;
    private javax.swing.JButton btnThemChiTiet;
    private javax.swing.JButton btnThemPhieu;
    private javax.swing.JButton btnXoaPhieu;
    private javax.swing.JComboBox cmbKhachHang;
    private javax.swing.JComboBox cmbTenSach;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tbChiTiet;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtNXB;
    private javax.swing.JTextField txtNamXB;
    private javax.swing.JTextField txtNgayLap;
    private javax.swing.JTextField txtNhanVien;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtThanhTien;
    private javax.swing.JTextField txtTienNo;
    private javax.swing.JTextField txtTienTra;
    private javax.swing.JTextField txtToKho;
    // End of variables declaration//GEN-END:variables

}
